package com.roncoo.education.db.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.AdminSystemRequest;
import com.roncoo.education.db.dao.AdminSystemDao;
import com.roncoo.education.db.entity.AdminSystem;
import com.roncoo.education.util.MyJwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminSystemService {

    @Resource
    private AdminSystemDao adminSystemDao;

    @Resource
    private MyJwtUtil jwtUtil;


    public String login(String username, String password) {
        AdminSystem query = new AdminSystem();
        query.setUsername(username);
        query.setPassword(DigestUtil.md5Hex(password));
        query.setDelFlag(1);
        List<AdminSystem> admins = adminSystemDao.findByBean(query);
        if(CollectionUtil.isNotEmpty(admins)) {
            AdminSystem admin = admins.get(0);
            return jwtUtil.generateToken(admin.getId(),admin.getUsername());
        }
        return null;
    }

    public Map<String,String> saveAdmin(AdminSystemRequest req){
        Map<String,String> ret = new HashMap<>();
        AdminSystem admin = BeanUtil.copyProperties(req,AdminSystem.class);
        AdminSystem query = new AdminSystem();
        query.setUsername(req.getUsername());
        List<AdminSystem> list = adminSystemDao.findByBean(query);
        if(CollUtil.isNotEmpty(list)){
            if(admin.getId()!=null){
                for (AdminSystem product1 : list) {
                    if(!product1.getId().equals(admin.getId())){
                        ret.put("isError", "The name already exists");
                        ret.put("code", "998");
                        return ret;
                    }
                }
            }else {
                ret.put("isError", "The name already exists");
                ret.put("code", "998");
                return ret;
            }
        }
        Long pid = 0l;
        if(admin.getId()!=null){
            pid= admin.getId();
            adminSystemDao.updateById(admin);
        }else{
            admin.setDelFlag(1);
            adminSystemDao.insert(admin);
            pid = admin.getId();
        }
        ret.put("isError", "success");
        ret.put("id", String.valueOf(pid));
        ret.put("code", "1");
        return ret;
    }

    public AdminSystem getAdmin(Long id){
        return adminSystemDao.findById(id);
    }

    public PageInfo<AdminSystem> getAdminList(AdminSystemRequest admin){
        int pageNum = admin.getPageNum();
        int pageSize = admin.getCount();
        if (admin.getPageNum() == null || admin.getPageNum() <= 0 || admin.getCount() == null) {
            pageNum = 1;
            pageSize = 20;
        }
        AdminSystem query = new AdminSystem();
        query.setUsername(admin.getUsername());
        query.setPhone(admin.getPhone());
        query.setDelFlag(1);
        PageHelper.startPage(pageNum, pageSize);
        List<AdminSystem> admins = adminSystemDao.findByBean(query);
        return new PageInfo<>(admins);
    }
}
